package cn.ibizlab.util.adapter.util.helper;

import cn.ibizlab.util.adapter.service.SysRuntimeModelService;
import cn.ibizlab.util.domain.IEntity;
import cn.ibizlab.util.helper.BeanCache;
import net.ibizsys.central.dataentity.IDataEntityRuntime;
import net.ibizsys.central.util.IEntityDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DataEntityUtil {

    @Autowired
    SysRuntimeModelService sysRuntimeModelService;

    /**
     * 创建entityDto
     * @param entity
     * @return
     */
    public IEntityDTO createEmptyEntityDto(IEntity entity){
        IDataEntityRuntime dataEntityRuntime = sysRuntimeModelService.getDataEntityRuntime(entity.getClass(),false);
        return dataEntityRuntime.createEntity();
    }

    /**
     * 查询实体类信息
     * @param entity
     * @return
     */
    public static BeanCache.BeanSchema getEntitySchema(IEntity entity){

        BeanCache.BeanSchema entitySchema = BeanCache.get(entity.getClass());
        if(entitySchema == null)
            throw new RuntimeException(String.format("未查询到实体类[%1$s]信息",entity.getClass().getSimpleName()));

        return entitySchema;
    }

}
